package cn.com.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author axie
 * @ClassName: TimeUtil
 * @Description： 时间工具类
 * @date 2022/10/01
 * @Version 1.0.0
 **/
public class TimeUtil {

    //获取当日00：00：00
    public static long getTodayStartTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime().getTime();
    }

    //获取当日23：59：59
    public static long getTodayEndTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime().getTime();
    }

    //    //获取当日23：59：59
//    public static long getYearEndTime(){
//        Calendar calendar=Calendar.getInstance();
//        calendar.setTime(new Date());
//        calendar.set(Calendar.MONTH,12);
//        calendar.set(Calendar.DAY_OF_MONTH,30);
//        calendar.set(Calendar.HOUR_OF_DAY,23);
//        calendar.set(Calendar.MINUTE,59);
//        calendar.set(Calendar.SECOND,59);
//        return calendar.getTime().getTime();
//    }
    //获取几天之后的日期
    public static Date getDateAfter(int day) {
        Calendar newDate = Calendar.getInstance();
        newDate.setTime(new Date());
        newDate.set(Calendar.DATE, newDate.get(Calendar.DATE) + day);
        return newDate.getTime();
    }

    // +1 几个月之后的时间
    public static Date getDateAfterMonth(int month) {
        Calendar newDate = Calendar.getInstance();
        newDate.setTime(new Date());
//        newDate.set(Calendar.DATE, newDate.get(Calendar.MONTH) + month);
        newDate.add(Calendar.MONTH, month);
        return newDate.getTime();
    }

    /**
     * 获取当年的第一天
     */
    public static Date getCurrentFirstOfYear() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);

        return getFirstOfYear(currentYear);
    }

    /**
     * 获取当年的最后一天
     */
    public static Date getCurrentLastOfYear() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getLastOfYear(currentYear);
    }


    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getFirstOfYear(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        return calendar.getTime();
    }


    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getLastOfYear(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTime();
    }

    /**
     * 获取当月最后一天时间
     *
     * @return
     */
    public static String getLastMonthDay() {
        // 本月末尾
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar thisMonthEndDateCal = Calendar.getInstance();
        thisMonthEndDateCal.set(Calendar.DAY_OF_MONTH, thisMonthEndDateCal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String thisMonthEndTime = format.format(thisMonthEndDateCal.getTime()) + " 23:59:59";
        return thisMonthEndTime;
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurrentTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = format.format(new Date());
        return currentTime;
    }

    public static String getCurrentDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String currentTime = format.format(new Date());
        return currentTime;
    }

    public static String getCurrentTimeStr() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
        return sdf.format(new Date());

    }

    public static String formatDateToString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        return sdf.format(new Date());


    }

    public static String DateTimeToString(Date date) {
        //设置时间格式
        String dateformat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat f = new SimpleDateFormat(dateformat);
        //转换成对应的时间格式
        String dateStr = f.format(date);
        return dateStr;
    }


    public static int daysBetween(Date date1, Date date2) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    public static int daysBetween(String date1str, String date2str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = null;
        int a = 0;
        try {
            date1 = format.parse(date1str);
            Date date2 = format.parse(date2str);
            a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
            return a;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return a;

    }


}
